package feng.demo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import feng.demo.dao.PermDao;
import feng.demo.dao.UserDao;
import feng.demo.entity.Perm;
import feng.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserDao userDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    PermService permService;
    @Override
    public User findByPhone(String phone) {
        User user = userDao.findByPhone(phone);

        return doadd(user);
    }

    @Override
    public List<User> findAll() {
        List<User> list = userDao.findAll();
        return doadds(list);
    }

    @Override
    public List<User> findByPermid(Integer permid) {
        List<User> users = userDao.findByPermid(permid);
        return doadds(users);
    }

    @Override
    public User findByUid(Integer uid) {
        User user = userDao.findByUid(uid);
        return doadd(user);
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int del(Integer uid) {
        return userDao.del(uid);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int findCount(Integer permid) {
        return userDao.findCount(permid);
    }


    @Override
    public List<User> findAllLimit(Integer pagenum, Integer lines) {
        List<User> users = userDao.findAllLimit(pagenum, lines);
        return doadds(users);
    }

    @Override
    public List<User> findByPermidLimit(Integer permid, Integer pagenum, Integer lines) {
        List<User> list = userDao.findByPermidLimit(permid, pagenum, lines);
        return doadds(list);
    }

    private User addRedis(User user){
        try {
            String permid = objectMapper.writeValueAsString(user.getPerm().getPermid());
            Object obj = redisTemplate.opsForHash().get("perm", permid);
            Perm perm = objectMapper.readValue((String) obj, Perm.class);
            user.setPerm(perm);
        }catch (Exception e){

        }
        return user;
    }

    @Override
    public User findByAccount(String account) {
        User user = userDao.findByAccount(account);

        return doadd(user);
    }

    @Override
    public List<User> findByUser(User user) {
        List<User> users = userDao.findByUser(user);
        return doadds(users);
    }

    @Override
    public User findBypwd(User user) {
        User u = userDao.findBypwd(user);
        if (u==null)return null;
        return doadd(u);
    }

    private List<User> doadds(List<User>list){
        for (User u:list) {
            Perm perm = permService.findById(u.getPerm().getPermid());
            u.setPerm(perm);
        }
        return list;
    }
    private User doadd(User u){
        if (u==null)return null;
        Perm perm = permService.findById(u.getPerm().getPermid());
        u.setPerm(perm);
        return u;
    }
}
